package cn.tedu.carehome.foundation.controller;


import cn.tedu.carehome.foundation.pojo.dto.*;
import cn.tedu.carehome.foundation.pojo.entity.FoodPackage;
import cn.tedu.carehome.foundation.pojo.entity.FoodPackageLink;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.restful.WebConsts;
import cn.tedu.carehome.foundation.service.IFoodPackageService;
import cn.tedu.carehome.foundation.service.IFoodService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/food-packages")
@Api(tags = "3.套餐管理与老人套餐关联模块")
public class FoodPackageController {

    @Autowired
    private IFoodPackageService foodPackageService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('food-package/add-new')")
    @ApiOperation("添加套餐")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid FoodPackageAddNewDTO foodPackageAddNewDTO){
        log.debug("开始处理添加餐品请求,参数:{}",foodPackageAddNewDTO);
        foodPackageService.addNew(foodPackageAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('food-package/delete')")
    @ApiOperation("根据id删除套餐")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "套餐ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> deleteById(@Range(min = 1, max = 10000, message = "删除套餐失败，餐品ID非法！")
                         @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除餐品】的请求，参数：{}", id);
        foodPackageService.deleteById(id);
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改套餐")
    @PreAuthorize("hasAuthority('food-package/update')")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(@Valid FoodPackageUpdateDTO foodPackageUpdateDTO) {
        log.debug("开始处理修改餐品请求,参数:{}",foodPackageUpdateDTO);
        foodPackageService.update(foodPackageUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation(value="根据id请求套餐信息")
    @PreAuthorize("hasAuthority('food-package/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id")
    public JsonResult<FoodPackageUpdateVO> selectById(@Range(min = 1, max = 10000, message = "删除套餐失败，套餐ID非法！")
                                         @PathVariable Long id){
        log.debug("查询的id是:id={}",id);
        FoodPackageUpdateVO foodPackageUpdateVO=foodPackageService.selectById(id);
        return JsonResult.ok(foodPackageUpdateVO);
    }
    @GetMapping("/list-by-page")
    @ApiOperation(value="根据分页请求套餐列表")
    @PreAuthorize("hasAuthority('food-package/read')")
    @ApiOperationSupport(order = 200)
    public JsonResult<JsonPage<FoodPackageAdminVO>>  selectFoodPackageByPage(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize
    ){


        log.debug("开始处理后台请求餐品列表");
        JsonPage<FoodPackageAdminVO> list = foodPackageService.queryFoodPackageListByPage(page,pageSize);
        return JsonResult.ok(list);
    }





    @GetMapping("/list")
    @ApiOperation(value="请求套餐列表")
    @PreAuthorize("hasAuthority('food-package/read')")
    @ApiOperationSupport(order = 200)
    public JsonResult<List<FoodPackageAdminVO>>  select(){
        log.debug("开始处理后台请求餐品列表");
        List<FoodPackageAdminVO> list = foodPackageService.queryFoodPackageList();
        return JsonResult.ok(list);
    }
    /**
     *
     * 以下为关联的模块
     *
     */

    @PostMapping("/link/add-new")
    @PreAuthorize("hasAuthority('food-package/add-new')")
    @ApiOperation("添加老人套餐关联")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNewLink(@Valid OlderFoodPackageAddNewDTO olderFoodPackageAddNewDTO){
        log.debug("开始处理添加老人套餐请求,参数:{}",olderFoodPackageAddNewDTO);
        foodPackageService.addNewLink(olderFoodPackageAddNewDTO);
        return JsonResult.ok();
    }


    @PostMapping("/link/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('food-package/delete')")
    @ApiOperation("根据id删除老人套餐关联")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "关联ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> deleteLinkById(@Range(min = 1, max = 10000, message = "删除关联失败，关联ID非法！")
                                       @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除关联】的请求，参数：{}", id);
        foodPackageService.deleteLinkById(id);
        return JsonResult.ok();
    }


    @PostMapping("/link/update")
    @PreAuthorize("hasAuthority('food-package/update')")
    @ApiOperation("修改关联")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateLink(@Valid OlderFoodPackageUpdateDTO olderFoodPackageUpdateDTO) {
        log.debug("开始处理修改关联请求,参数:{}",olderFoodPackageUpdateDTO);
        foodPackageService.updateLink(olderFoodPackageUpdateDTO);
        return JsonResult.ok();
    }


    @GetMapping("/link/{id:[0-9]+}")
    @ApiOperation(value="根据id请求关联信息")
    @PreAuthorize("hasAuthority('food-package/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id")
    public JsonResult<OlderFoodPackageStandardVO> selectLinkById(@Range(min = 1, max = 10000, message = "查询关联失败，关联ID非法！")
                                                      @PathVariable Long id){
        log.debug("查询的id是:id={}",id);
        OlderFoodPackageStandardVO olderFoodPackageStandardVO=foodPackageService.selectStandardLinkById(id);
        return JsonResult.ok(olderFoodPackageStandardVO);
    }

    @GetMapping("/link/list")
    @ApiOperation(value="请求关联列表")
    @PreAuthorize("hasAuthority('food-package/read')")
    @ApiOperationSupport(order = 200)
    public JsonResult<List<OlderFoodPackageLinkListItemVO>>  selectLink(){
        log.debug("开始处理后台请求关联列表");
        List<OlderFoodPackageLinkListItemVO> list = foodPackageService.selectLink();
        return JsonResult.ok(list);
    }


    @GetMapping("/link/food_package/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('food-package/update')")
    @ApiOperation(value="根据套餐id请求餐品信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id")
    public JsonResult<List<FoodPackageFoodStandardVO>> selectFoodByFoodPackage(@Range(min = 1, max = 10000, message = "查询套餐失败，套餐ID非法！")
                                                                 @PathVariable Long id){
        log.debug("查询的id是:id={}",id);
        List<FoodPackageFoodStandardVO> list = foodPackageService.selectFoodByFoodPackage(id);
        return JsonResult.ok(list);
    }

}
